#include "xtypes.h"
#include "xaux.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "xstring.h"



ulong_t xGetSecondsFrom1970()
{
	return (long_t)time(NULL);
}

ulong_t xGetTickCount()
{
	return (long_t)clock();
}

long_t xGetFileLength(lpcstr_t lpszPathName)
{
	FILE *fp;
	long_t nSizeFile;
	fp = fopen( lpszPathName, "rb" );

	fseek( fp, 0, SEEK_END );
	nSizeFile = ftell( fp );
	rewind( fp );

	fclose (fp);

	return nSizeFile;
}





bool_t xIsLittleEndian()
{
	static const dword_t nData = 0x12345678;
	byte_t * p = (byte_t*)(&nData);

	return (p[0]==0x78) ? TRUE:FALSE;
}





void xLogConsole( lpcstr_t szInfo )
{
	printf("- %s\n", szInfo);
}

void xLogFile( lpcstr_t szInfo, lpcstr_t szFileName )
{
	FILE* fp;
	fp = fopen(szFileName, "a");

	fprintf(fp, "- %s\n", szInfo);

	fclose(fp);
}

bool_t xIsFileExist( lpcstr_t lpszPathName )
{
	FILE *fp;
	fp = fopen(lpszPathName, "r" );

	if(fp!=NULL)
	{
		fclose(fp);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void xLoadMemoryBlock( memptr_t _DataPtr, int_t _Size, lpcstr_t lpszPathName )
{
	FILE *fp;
	int_t nLength = xGetFileLength(lpszPathName);
	int_t nLastLength = AUX_MIN( nLength, _Size );
	fp = fopen(lpszPathName, "rb" );
	fread(_DataPtr, 1, _Size, fp);
	fclose (fp);
}


void xSaveMemoryBlock( memptr_t _DataPtr, int_t _Size, lpcstr_t lpszPathName )
{
	FILE *fp;
	fp = fopen( lpszPathName, "wb" );
	fwrite( _DataPtr, 1, _Size, fp );
	fclose (fp);
}




int_t xConvertStringToInteger(const CxString& _Str )
{
	int_t ret = 0;
	int_t len = _Str.length();
	

	if(len<=2)
	{
		return atoi(_Str.c_str());
	}

	CxString strTmp;
	strTmp = _Str.substr(0,2);
	strTmp = xConvertStringToLowerCase(strTmp);

	if(strTmp=="0x")
	{
		sscanf( _Str.c_str(), "0x%x", &ret );

	}
	else if(strTmp[0]=='0')
	{
		sscanf( _Str.c_str(), "0%o", &ret );
	}

	return ret;
}

CxString xTrimString( const CxString& _Str )
{
	CxString strTmp;
	int_t nRawLen = _Str.length();

	CxStaticBuffer buf( nRawLen+1);
	strcpy((char *)buf.data(), _Str.c_str());

	char *spca=NULL, *epca=NULL;
	spca = (char *)buf.data();
	epca = spca + nRawLen;

	while(*spca==' ' && spca<epca) spca++;
	while(*(epca-1)==' ' && spca<epca) epca--;
	*epca = 0;

	strTmp.assign((const char*)spca);
	return strTmp;
}


CxString xConvertStringToUpperCase( const CxString& _Str )
{
	CxString strTmp;
	for(int_t i=0,n=_Str.length(); i<n; ++i)
	{
		char_t ch = _Str.at(i);
		if( ch>='a' && ch<='z' )
		{
			ch -= 32;			
		}

		strTmp += ch;
	}
	return strTmp;
}

CxString xConvertStringToLowerCase( const CxString& _Str )
{
	CxString strTmp;
	for(int_t i=0,n=_Str.length(); i<n; ++i)
	{
		char_t ch = _Str.at(i);
		if( ch>='A' && ch<='Z' )
		{
			ch += 32;			
		}

		strTmp += ch;
	}
	return strTmp;
}

CxString xStringCapitalize( const CxString& _Str )
{

	return _Str;
}

CxString xStringInvertCase( const CxString& _Str )
{
	CxString strTmp;
	for(int_t i=0,n=_Str.length(); i<n; ++i)
	{
		char_t ch = _Str.at(i);
		if( ch>='a' && ch<='z' )
		{
			ch -= 32;
		}
		else if( ch>='A' && ch<='Z' )
		{
			ch += 32;
		}

		strTmp += ch;
	}
	return strTmp;
}



